package com.umakr.ax.app.service;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.UserRelationMapper;
import com.umakr.ax.common.model.UserRelation;
import com.umakr.ax.common.service.UserRelationService;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.security.model.User;
import com.umakr.ax.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author tangtengfei
 */
@Service
public class UserWebRelationService extends BaseService<UserRelationMapper, UserRelation> {

    @Autowired
    UserWebService userWebService;

    @Autowired
    UserService userService;

    @Autowired
    UserRelationService userRelationService;

    public Object listAccountByRsId(Long rsId) {
        Page<UserRelation> page = baseMapper.listAccountByRsId(rsId);
        return  page;
    }

    public Object saveOrUpdateAccount(Long userId,Long rsId, String account, String nickname, String password, Integer
            roleName,Integer status,Integer type) {
        //type 区分仅修改密码或编辑功能  0 仅修改密码  1 编辑
        String msg = "";
        userWebService.addRsAdmin(userId,account,password,nickname,rsId,roleName,status,type);
        if(userId == null) {
            msg = "新增成功";
        } else {
            msg = "修改成功";
        }
        return resultMap(1,msg);
    }

    public Object delectAccount(Long userId) {
        String msg = "";
        baseMapper.delByUserId(userId);
        userWebService.delectRsAdmin(userId);
        msg = "删除成功";
        return resultMap(1,msg);
    }

    public Object validateAccountOnly(String account) {
        String msg = "";
        EntityWrapper<User> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account",account);
        User user = userService.selectOne(userWrapper);

        if(user == null) {
           return resultMap(1,msg);
        } else {
            EntityWrapper<UserRelation> userRelationWrapper = new EntityWrapper<>();
            userRelationWrapper.eq("user_id",user.getId());
            UserRelation userRelation = userRelationService.selectOne(userRelationWrapper);
            if(userRelation == null) {
                return resultMap(1,msg);
            } else {
                return resultMap(0,"该账号已被使用");
            }
        }
    }
}
